package com.longrise.android.byjk.plugins.dealsituation.course.video2.defend;

import android.os.Handler;
import android.os.Message;

import com.longrise.android.byjk.plugins.dealsituation.course.video2.dialog.UploadDialog;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.StudyRecordBase;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.study.DefendHandler;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.study.LoopRecord;
import com.longrise.common.utils.PrintLog;


/**
 * Created by godliness on 2017/8/4.
 * 通过构建一个新的该类实例，实现维护学习记录过程逻辑
 *
 * 具体查看维护过程流程图
 */

public final class DefendStudy {

    private static final String TAG = "DefendStudy";

    private int mDefend = 0;
    private int mMaxDefend;

    private int mHand = 0;
    private int mMaxHand;

    /**
     * 是否手动同步
     */
    private boolean mAccessRecord;

    private DefendStudyCallback mStudyCallback;

    public DefendStudy(int maxDefend, int maxHand) {
        mMaxDefend = maxDefend;
        mMaxHand = maxHand;
        mAccessRecord = false;
    }

    public DefendStudy(DefendStudyCallback callback) {
        this(3, 2);
        this.mStudyCallback = callback;
    }

    public DefendStudy() {
        this(3, 2);
    }

    /**
     * 一般配合onPrepared()，既视频初始化完成时调用该方法
     * 将mCallback添加到DefendHandler中
     */
    public void monitor() {
        DefendHandler.getInstance().monitor(mCallback);
    }

    /**
     * delay 60000ms 执行一次  一般配合Activity的onResume()
     */
    public void start() {
        boolean isStart = DefendHandler.getInstance().start(DefendHandler.MSG_LOOPER_DELAY);
        if (!isStart) {
            PrintLog.e(TAG, "start: false");
        }
    }

    /**
     * 停止维护过程  一般配合Activity的onStop()
     */
    public void stop() {
        DefendHandler.getInstance().stop();
    }

    /**
     * 手动同步，一旦失败，立即弹窗
     */
    public void manualPreformStart() {
        stop();
        mAccessRecord = true;
        preformStart();
    }

    /**
     * 执行
     */
    private void preformStart() {
        if (mStudyCallback != null) {
            LoopRecord.loop(mStudyCallback.getRecord(), mOnDefend);
        }
    }

    /**
     * start()之后，每delay 60000ms 会调用一次handleMessage()
     */
    private Handler.Callback mCallback = new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            preformStart();
            return true;
        }
    };

    /**
     * 处理维护过程结果
     */
    private DefendHandler.OnDefend mOnDefend = new DefendHandler.OnDefend() {
        @Override
        public void onDefend(boolean videopass) {
            if (videopass) {
                mAccessRecord = false;
                stop();
            }
            if (mStudyCallback != null) {
                mStudyCallback.onDefend(videopass);
            }
            if (mAccessRecord && !videopass) {
                mAccessRecord = false;
                start();
            }
            reset();
        }

        @Override
        public void onError() {
            if (mAccessRecord) {
                accessRecordIsTrue();
            } else {
                accessRecordIsFalse();
            }
        }
    };

    private void accessRecordIsFalse() {
        mDefend++;
        if (mDefend >= mMaxDefend) {
            stop();
            reset();
            if (mStudyCallback != null) {
                mStudyCallback.onError(UploadDialog.DIALOG_TYPE_ONE);
            }
            mAccessRecord = true;
        } else {
//            if (mDefend == (mMaxDefend - 1)) {
//                stop();
//            }
            //继续执行...
        }
    }

    private void accessRecordIsTrue() {
        mHand++;
        if (mHand >= mMaxHand) {
            reset();
            if (mStudyCallback != null) {
                mStudyCallback.onError(UploadDialog.DIALOG_TYPE_THR);
            }
        } else {
            if (mStudyCallback != null) {
                mStudyCallback.onError(UploadDialog.DIALOG_TYPE_SEC);
            }
        }
    }

    private void reset() {
        mDefend = 0;
        mHand = 0;
    }

    public interface DefendStudyCallback {

        /**
         * 维护过程记录，所需要的参数
         *
         * @return Record
         */
        StudyRecordBase.Record getRecord();

        /**
         * 维护过程记录
         *
         * @param videopass 当前课件是否通过
         */
        void onDefend(boolean videopass);

        /**
         * 维护过程记录
         * 自动维护过程记录的过成功，当出错次数大于等于3次时，回调该方法。根据错误类型显示出错对话框
         *
         * @param errorType 出错类型，根据该出错类型弹出对象出错处理对话框
         *                  UploadDialog.DIALOG_TYPE_ONE
         *                  UploadDialog.DIALOG_TYPE_TWO
         *                  UploadDialog.DIALOG_TYPE_THR
         */
        void onError(int errorType);
    }

    public void setStudyCallback(DefendStudyCallback callback) {
        this.mStudyCallback = callback;
    }
}
